package com.yc.springmvc.web;


import org.springframework.core.convert.converter.Converter;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class DateConvertor implements Converter<String, Date> {
    public static final SimpleDateFormat[] YYYYMMDDS = {
            new SimpleDateFormat("yyyy-MM-dd"),
            new SimpleDateFormat("yyyy/MM/dd"),
            new SimpleDateFormat("MM-dd-yyyy")
    };

    @Override
    public Date convert(String source) {
        Date ret = null;
        for (SimpleDateFormat yyyymmdd : YYYYMMDDS) {
            try {
                ret = yyyymmdd.parse(source);
                break;
            } catch (ParseException e) {
                continue;
            }
        }
        return ret;
    }
}
